package yunjiao.javatutorials.designpatterns.combinator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 校验结构
 *
 * @author yangyunjiao
 */
public interface ValidationResult {
    boolean isValid();

    List<String> getErrors(); // 收集所有错误信息

    // 可以有一个静态方法创建成功和失败的结果
    static ValidationResult valid() {
        return new SucessValidationResult();
    }
    static ValidationResult invalid(String error) {
        return new FailureValidationResult(List.of(error));
    }

    static ValidationResult invalid(List<String> errors) {
        return new FailureValidationResult(errors);
    }

    class SucessValidationResult implements ValidationResult {
        @Override
        public boolean isValid() {
            return true;
        }

        @Override
        public List<String> getErrors() {
            return Collections.emptyList();
        }
    }

    class FailureValidationResult implements ValidationResult {
        private final List<String> errors = new ArrayList<>();

        public FailureValidationResult(List<String> errors) {
            this.errors.addAll(errors);
        }

        @Override
        public boolean isValid() {
            return false;
        }

        @Override
        public List<String> getErrors() {
            return errors;
        }
    }
}


